/**
 * @(#)TaskInstanceQueryDAO.java Aug 31, 2009
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.repository.DeploymentProperty;
import org.jbpm.pvm.internal.task.ParticipationImpl;
import org.jbpm.pvm.internal.task.TaskImpl;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.dao.api.ITaskInstanceQueryDAO;
import com.wilmar.process.entity.query.ProcessTakenTaskQueryObject;
import com.wilmar.process.entity.query.ProcessUnassignTaskQueryObject;
import com.wilmar.process.enumerate.EnumSystemQueryNameSet;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0
 */
public class TaskInstanceQueryDAOImpl extends BaseDAOImpl<Task, String>
		implements ITaskInstanceQueryDAO {

	/**
	 * @see com.wilmar.process.dao.api.ITaskInstanceQueryDAO#getTakenTaskCountByUserId(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public Long getTakenTaskCountByUserId(String userId) {
		List<Long> resultList = super
				.getHibernateTemplate()
				.findByNamedQueryAndNamedParam(
						EnumSystemQueryNameSet.PROCESS_SYSTEM_QUERY_NAME_TASKINSTANCECOUNT_GETTAKENCOUNTBYUSER
								.getValue(), "assignee", userId);
		if (resultList == null || resultList.size() == 0)
			return 0L;
		else {
			Object result = resultList.get(0);
			return (Long) result;
		}
	}

	/**
	 * @see com.wilmar.process.dao.api.ITaskInstanceQueryDAO#getUnAssignedTaskCountByIdSet(java.util.Set)
	 */
	@SuppressWarnings("unchecked")
	public Long getUnAssignedTaskCountByIdSet(Set<String> idSet) {
		List<Long> resultList = super
				.getHibernateTemplate()
				.findByNamedQueryAndNamedParam(
						EnumSystemQueryNameSet.PROCESS_SYSTEM_QUERY_NAME_TASKINSTANCECOUNT_GETUNASSIGNEDCOUNTBYIDSET
								.getValue(), "candidateIds", idSet);
		if (resultList == null || resultList.size() == 0)
			return 0L;
		else {
			return resultList.get(0);
		}
	}

	/**
	 * @see com.wilmar.process.dao.api.ITaskInstanceQueryDAO#getUnassignTaskListByQueryObject(com.wilmar.process.entity.query.ProcessUnassignTaskQueryObject,
	 *      java.util.Set, java.util.Set)
	 */
	public List<Task> getUnassignTaskListByQueryObject(
			ProcessUnassignTaskQueryObject queryObject, Set<String> userIdSet) {
		StringBuffer hql = new StringBuffer();
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_SELECT);
		hql.append("task");
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_FROM);
		hql.append(TaskImpl.class.getName());
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
		hql.append("task");
		hql.append(ProcessSystemDeclareKeys.COMMA);
		hql.append(ParticipationImpl.class.getName());
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
		hql.append("participant");
		if ((queryObject.getProcessDefinitionId() != null
				&& queryObject.getProcessDefinitionId().length != 0 && queryObject
				.getProcessDefinitionId()[0].trim().length() != 0)
				|| (queryObject.getProcessDefinitionName() != null
						&& queryObject.getProcessDefinitionName().length != 0 && queryObject
						.getProcessDefinitionName()[0].trim().length() != 0)
				|| (queryObject.getProcessDefinitionKey() != null
						&& queryObject.getProcessDefinitionKey().length != 0 && queryObject
						.getProcessDefinitionKey()[0].trim().length() != 0)) {
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ExecutionImpl.class.getName());
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
			hql.append("execution");
		}
		if ((queryObject.getProcessDefinitionName() != null
				&& queryObject.getProcessDefinitionName().length != 0 && queryObject
				.getProcessDefinitionName()[0].trim().length() != 0)
				|| (queryObject.getProcessDefinitionKey() != null
						&& queryObject.getProcessDefinitionKey().length != 0 && queryObject
						.getProcessDefinitionKey()[0].trim().length() != 0)) {
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(DeploymentProperty.class.getName());
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
			hql.append("deployment");
		}
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_WHERE);
		hql.append("participant.task");
		hql.append(ProcessSystemDeclareKeys.EQUAL);
		hql.append("task");
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
		hql.append("participant.type");
		hql.append(ProcessSystemDeclareKeys.EQUAL);
		hql.append("'candidate'");

		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
		hql.append("participant.userId");
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
		hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);

		boolean uIdFlog = false;
		Iterator<String> uIdIterator = userIdSet.iterator();
		while (uIdIterator.hasNext()) {
			uIdFlog = true;
			String uId = uIdIterator.next();
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(uId.trim());
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
		}
		if (uIdFlog) {
			hql.deleteCharAt(hql.length() - 1);
		}
		hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
		hql.append("task.assignee");
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IS);
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_NULL);
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
		hql.append("task.state");
		hql.append(ProcessSystemDeclareKeys.EQUAL);
		hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
		hql.append(Task.STATE_OPEN);
		hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
		hql.append("task.isSignalling");
		hql.append(ProcessSystemDeclareKeys.NOTEQUAL);
		hql.append(0);

		SimpleDateFormat sdf = new SimpleDateFormat(
				ProcessSystemDeclareKeys.SYSTEM_LONG_DATE_FORMAT
						+ ProcessSystemDeclareKeys.SPACE
						+ ProcessSystemDeclareKeys.SYSTEM_LONG_TIME_FORMAT);

		if (queryObject.getTaskName() != null
				&& queryObject.getTaskName().length != 0
				&& queryObject.getTaskName()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.name");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String s : queryObject.getTaskName()) {
				if (s != null && s.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(s.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}

		if (queryObject.getProcessInstanceId() != null
				&& queryObject.getProcessInstanceId().length != 0
				&& queryObject.getProcessInstanceId()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.executionId");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String s : queryObject.getProcessInstanceId()) {
				if (s != null && s.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(s.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}

		if (queryObject.getMaxStartTime() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.createTime");
			hql.append(ProcessSystemDeclareKeys.LESS_EQUAL);
			hql.append("to_date");
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(sdf.format(queryObject.getMaxStartTime()));
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("yyyy-mm-dd hh24:mi:ss");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (queryObject.getMinStartTime() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.createTime");
			hql.append(ProcessSystemDeclareKeys.GREATER_EQUAL);
			hql.append("to_date");
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(sdf.format(queryObject.getMinStartTime()));
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("yyyy-mm-dd hh24:mi:ss");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (queryObject.getProcessDefinitionName() != null
				&& queryObject.getProcessDefinitionName().length != 0
				&& queryObject.getProcessDefinitionName()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.processInstance");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append("execution");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("execution.processDefinitionId");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append("deployment.stringValue");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("deployment.key");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("pdid");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("deployment.objectName");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String pdn : queryObject.getProcessDefinitionName()) {
				if (pdn != null && pdn.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(pdn.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (queryObject.getProcessDefinitionKey() != null
				&& queryObject.getProcessDefinitionKey().length != 0
				&& queryObject.getProcessDefinitionKey()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.processInstance");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append("execution");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("execution.processDefinitionId");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append("deployment.stringValue");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("deployment.key");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("pdid");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("deployment.objectName");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_SELECT);
			hql.append("deplprop.objectName");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_FROM);
			hql.append(DeploymentProperty.class.getName());
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
			hql.append("deplprop");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_WHERE);
			hql.append("deplprop.key");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("pdkey");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("deplprop.stringValue");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String pdkey : queryObject.getProcessDefinitionKey()) {
				if (pdkey != null && pdkey.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(pdkey.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}

		if (queryObject.getProcessDefinitionId() != null
				&& queryObject.getProcessDefinitionId().length != 0
				&& queryObject.getProcessDefinitionId()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.processInstance");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append("execution");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("execution.processDefinitionId");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			boolean pdFlog = false;

			for (String pdid : queryObject.getProcessDefinitionId()) {
				if (pdid != null && pdid.trim().length() != 0) {
					pdFlog = true;
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(pdid.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			if (pdFlog) {
				hql.deleteCharAt(hql.length() - 1);
			}
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		System.out.println("HQL   ===========   " + hql.toString());
		return super.getListByHql(hql.toString());
	}

	/**
	 * @see com.wilmar.process.dao.api.ITaskInstanceQueryDAO#getTakenTaskListByQueryObject(com.wilmar.process.entity.query.ProcessTakenTaskQueryObject,
	 *      java.lang.String, java.util.Set)
	 */
	public List<Task> getTakenTaskListByQueryObject(
			ProcessTakenTaskQueryObject queryObject, String userId) {

		StringBuffer hql = new StringBuffer();
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_SELECT);
		hql.append("task");
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_FROM);
		hql.append(TaskImpl.class.getName());
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
		hql.append("task");
		if ((queryObject.getProcessDefinitionId() != null
				&& queryObject.getProcessDefinitionId().length != 0 && queryObject
				.getProcessDefinitionId()[0].trim().length() != 0)
				|| (queryObject.getProcessDefinitionName() != null
						&& queryObject.getProcessDefinitionName().length != 0 && queryObject
						.getProcessDefinitionName()[0].trim().length() != 0)
				|| (queryObject.getProcessDefinitionKey() != null
						&& queryObject.getProcessDefinitionKey().length != 0 && queryObject
						.getProcessDefinitionKey()[0].trim().length() != 0)) {
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ExecutionImpl.class.getName());
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
			hql.append("execution");
		}
		if ((queryObject.getProcessDefinitionName() != null
				&& queryObject.getProcessDefinitionName().length != 0 && queryObject
				.getProcessDefinitionName()[0].trim().length() != 0)
				|| (queryObject.getProcessDefinitionKey() != null
						&& queryObject.getProcessDefinitionKey().length != 0 && queryObject
						.getProcessDefinitionKey()[0].trim().length() != 0)) {
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(DeploymentProperty.class.getName());
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
			hql.append("deployment");
		}
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_WHERE);
		hql.append("task.assignee");
		hql.append(ProcessSystemDeclareKeys.EQUAL);
		hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
		hql.append(userId);
		hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
		hql.append("task.state");
		hql.append(ProcessSystemDeclareKeys.EQUAL);
		hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
		hql.append(Task.STATE_OPEN);
		hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
		hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
		hql.append("task.isSignalling");
		hql.append(ProcessSystemDeclareKeys.NOTEQUAL);
		hql.append(0);

		SimpleDateFormat sdf = new SimpleDateFormat(
				ProcessSystemDeclareKeys.SYSTEM_LONG_DATE_FORMAT
						+ ProcessSystemDeclareKeys.SPACE
						+ ProcessSystemDeclareKeys.SYSTEM_LONG_TIME_FORMAT);

		if (queryObject.getTaskName() != null
				&& queryObject.getTaskName().length != 0
				&& queryObject.getTaskName()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.name");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String s : queryObject.getTaskName()) {
				if (s != null && s.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(s.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}

		if (queryObject.getProcessInstanceId() != null
				&& queryObject.getProcessInstanceId().length != 0
				&& queryObject.getProcessInstanceId()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.executionId");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String s : queryObject.getProcessInstanceId()) {
				if (s != null && s.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(s.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}

		if (queryObject.getMaxStartTime() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.createTime");
			hql.append(ProcessSystemDeclareKeys.LESS_EQUAL);
			hql.append("to_date");
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(sdf.format(queryObject.getMaxStartTime()));
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("yyyy-mm-dd hh24:mi:ss");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		if (queryObject.getMinStartTime() != null) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.createTime");
			hql.append(ProcessSystemDeclareKeys.GREATER_EQUAL);
			hql.append("to_date");
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(sdf.format(queryObject.getMinStartTime()));
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.COMMA);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("yyyy-mm-dd hh24:mi:ss");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}

		if (queryObject.getProcessDefinitionName() != null
				&& queryObject.getProcessDefinitionName().length != 0
				&& queryObject.getProcessDefinitionName()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.processInstance");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append("execution");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("execution.processDefinitionId");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append("deployment.stringValue");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("deployment.key");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("pdid");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("deployment.objectName");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String pdn : queryObject.getProcessDefinitionName()) {
				if (pdn != null && pdn.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(pdn.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}

		if (queryObject.getProcessDefinitionKey() != null
				&& queryObject.getProcessDefinitionKey().length != 0
				&& queryObject.getProcessDefinitionKey()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.processInstance");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append("execution");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("execution.processDefinitionId");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append("deployment.stringValue");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("deployment.key");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("pdid");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("deployment.objectName");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_SELECT);
			hql.append("deplprop.objectName");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_FROM);
			hql.append(DeploymentProperty.class.getName());
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AS);
			hql.append("deplprop");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_WHERE);
			hql.append("deplprop.key");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append("pdkey");
			hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("deplprop.stringValue");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			for (String pdkey : queryObject.getProcessDefinitionKey()) {
				if (pdkey != null && pdkey.trim().length() != 0) {
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(pdkey.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}

		if (queryObject.getProcessDefinitionId() != null
				&& queryObject.getProcessDefinitionId().length != 0
				&& queryObject.getProcessDefinitionId()[0].trim().length() != 0) {
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("task.processInstance");
			hql.append(ProcessSystemDeclareKeys.EQUAL);
			hql.append("execution");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_AND);
			hql.append("execution.processDefinitionId");
			hql.append(ProcessSystemDeclareKeys.SQL_STATEMENT_IN);
			hql.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
			boolean pdFlog = false;
			for (String pdid : queryObject.getProcessDefinitionId()) {
				if (pdid != null && pdid.trim().length() != 0) {
					pdFlog = true;
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(pdid.trim());
					hql.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
					hql.append(ProcessSystemDeclareKeys.COMMA);
				}
			}

			if (pdFlog) {
				hql.deleteCharAt(hql.length() - 1);
			}
			hql.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		}
		System.out.println("HQL   ===========   " + hql.toString());
		return super.getListByHql(hql.toString());
	}
}
